package com.example.demo.controller;
import com.example.demo.mapper.CommonMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.dao.JdbcMysql;
import com.example.demo.dao.SpringJdbcTemplate;
import com.example.demo.model.*;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.LinkedList;
import java.util.List;

@RestController
public class DemoController {
    @Autowired
    private JdbcMysql jdbcMysql;

    @Autowired
    private SpringJdbcTemplate springJdbcTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommonMapper commonMapper;

    @PostMapping("/add_test")
    public Response<String> addTest(@RequestBody Test test) {
        Response<String> response = new Response<>();
        try {
            test.setEnglishScore(89.2);
            commonMapper.insertSelective(test);
            response.setData("insert successfully");
        } catch (RuntimeException e) {
            response.setErrorNo(-1);
            response.setErrorMessage(e.getMessage());
        }
        return response;
    }

    @PostMapping("/delete_test")
    public Response<String> deleteTest(@RequestBody Test test)  {
        Response<String> response=new Response<>();
        try{
            commonMapper.delete(test);
            response.setData("delete successfully");
        }catch (RuntimeException e){
            response.setErrorNo(-1);
            response.setErrorMessage(e.getMessage());
        }
        return response;
    }

    @PostMapping("/change_test")
    public Response<String> changeTest(@RequestBody Test test)  {
       Response<String> response=new Response<>();
       try{
           commonMapper.updateByPrimaryKeySelective(test);
           response.setData("change successfully");
       }catch(RuntimeException e){
           response.setErrorNo(-1);
           response.setErrorMessage(e.getMessage());
       }
       return response;
    }

    @PostMapping("/get_testv2")
    public Response<List<Test>> getTestv2(@RequestBody TestV2 test)  {
        Response<List<Test>> response=new Response<>();
        try{
            Weekend<TestV2> wk = Weekend.of(TestV2.class);
            wk.weekendCriteria().andEqualTo(TestV2::getEnglishScore,test.getEnglishScore());
            response.setData(Lists.newArrayList(commonMapper.selectByExample(wk)));
        }catch(RuntimeException e){
            response.setErrorNo(-1);
            response.setErrorMessage(e.getMessage());
        }
        return response;
    }

    @PostMapping("/get_test")
    public Response<List<Test>> getTest(@RequestBody SearchReq req)  {
        Response<List<Test>> response=new Response<>();
        try{
            Example example =new Example(Test.class);
            Example.Criteria criteria= example.createCriteria();
            for(KeyValue keyValue:req.getColumnList()) {
                criteria.andEqualTo(keyValue.getProperty(),keyValue.getValue());
            }
            response.setData(Lists.newArrayList(commonMapper.selectByExample(example)));
        }catch(RuntimeException e){
            response.setErrorNo(-1);
            response.setErrorMessage(e.getMessage());
        }
        return response;
    }
}
